package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */

    @PostMapping("/logout")
    @ApiOperation(value = "退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param dto
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping
    public Result addEmp(@RequestBody EmployeeDTO dto){
        log.info("新增员工:{}",dto);
        employeeService.addEmp(dto);
        return Result.success();

    }

    /**
     * 员工管理分页-controller层
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result Page(EmployeePageQueryDTO dto){
        log.info("分页查询:{}",dto);
        PageResult result=employeeService.getPage(dto);
        return Result.success(result);

    }
    /**
     * 修改员工状态
     *
     **/
    @ApiOperation("根据id修改员工状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        employeeService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id获取单员工
     * @param id
     * @return
     */
    @ApiOperation("根据id获取员工")
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Long id) {
        return Result.success(employeeService.getEmpById(id));
    }
/**
 * 修改员工信息接口
 * 使用PUT方法进行请求，接收前端传来的EmployeeDTO对象作为参数
 * 调用employeeService的updateEmp方法更新员工信息
 * 操作成功后返回Result.success()表示操作成功
 *
 * @param dto 包含员工更新信息的DTO对象，通过@RequestBody注解接收请求体中的JSON数据
 * @return 返回操作结果，成功时返回success状态
 */
    @ApiOperation("修改员工信息")
    @PutMapping
    public Result update(@RequestBody EmployeeDTO dto) {
    // 调用员工服务层的updateEmp方法执行更新操作
        employeeService.updateEmp(dto);
    // 返回成功结果
        return Result.success();
    }

    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO dto){
        employeeService.updatePassword(dto);
        return Result.success();
    }

}
